#!/bin/bash
usage="modifyvm <vmname> <ram> <hddsize> <cores> <vdiDir>"

if [ $# -eq 0 ]
then
	echo -e "\n Usage : $usage\n"
	exit 100
fi

echo -e "\n\tModifying VM\n"

cmd="VBoxManage modifyvm $1 --memory $2 --cpus $4"
echo -e "\n\t$cmd\n"
$cmd
if [ $? -ne 0 ]
then
	echo -e "\n\tFatal Error in $cmd\n"
	exit 2
fi

hddCount=`ls $5 | grep $1*vdi`
newSecondaryHdd="$5/$1HDD$hddCount.vdi"

cmd="VBoxManage createhd --filename $newSecondaryHdd --size $3 --register"
echo -e "\n\t$cmd\n"
$cmd

if [ $? -ne 0 ]
then
	echo -e "\n\tFatal Error in $cmd\n"
	
	cmd="rm $newSecondaryHdd"
	echo -e "\n\t$cmd\n"
	$cmd
	exit 6
fi



cmd="VBoxManage storageattach $1 --storagectl 'IDE Controller' --port 2 --device 0 --type hdd --medium $newSecondaryHdd"
echo -e "\n\t$cmd\n"
VBoxManage storageattach $1 --storagectl 'IDE Controller' --port 1 --device 0 --type hdd --medium $newSecondaryHdd

if [ $? -ne 0 ]
then
	echo -e "\n\tFatal Error in $cmd\n"
	cmd="rm $newSecondaryHdd"
	echo -e "\n\t$cmd\n"
	$cmd
	exit 7
fi

exit 0